import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { config } from '../../shared/smartadmin.config'

@Component({
  selector: 'sa-bom-manage',
  templateUrl: './bom-manage.component.html',
  styleUrls: ['./bom-manage.component.css']
})
export class BomManageComponent implements OnInit {
  tableData: any = [] // table的表单数据
  BomCodeInfo: any = {} // BOM详情页的要遍历对象
  totalCounts: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数

  activeSelected: string = '' //row高亮
  // 查询参数
  bomNumber: string = '' // BOM编码
  bomName: string = ''
  startTime: any = '' // 开始时间
  endTime: any = '' // 结束时间
  constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {})
  }
  ngOnInit() {
    this.load()
  }
  totalCountTab: number = 0
  pageSizeTab: number = 10
  curPageTab: number = 1 //分页当前页
  //初始化加载list数据
  load() {
    let param = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        bomNumber: this.bomNumber,
        bomName: this.bomName,
        startTime: this.startTime,
        endTime: this.endTime
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(param)
    this.jsonApiService.http('bom/v3/queryBomV3PageList.html', reqdata).subscribe(result => {
      if (result.isSuccess && result.exhibitDatas) {
        this.tableData = result.exhibitDatas
        this.totalCounts = result.recordCount
      }
    })
  }

  //时间选中回调
  datePickerSelected(time) {
    this.startTime = time.startTime
    this.endTime = time.endTime
  }
  delKey(type) {
    if (type == 'bomName') {
      this.bomName = ''
    }
    if (type == 'bomNumber') {
      this.bomNumber = ''
    }
  }
  // 查询
  tableSearch() {
    this.curPage = 1
    this.load()
  }
  // 显示导入的弹框
  showImportModal(modal) {
    modal.show()
  }
  // 关闭导入的弹框
  cancelUp(val) {
    val.hide()
    this.fileToUpload = []
    this.aliasFileToUpload = []
  }
  // excel 导入
  fileUp: string = ''
  fileToUpload: any = [] //上传文件回调参数
  // 清空input type=file输入框的值
  clearFile() {
    this.fileUp = ''
  }
  //excel文件上传change回调
  fileChange(files) {
    // 这一层循环判断格式是否正确
    for (let index = 0; index < files.length; index++) {
      const isEXCEL =
        files.item(index).type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ||
        files.item(index).type === 'application/vnd.ms-excel'
      if (!isEXCEL) {
        this.jsonApiService.message('上传文件只能是 excel格式!')
        return
      } else {
        let isSame = false
        for (let i = 0; i < this.fileToUpload.length; i++) {
          if (this.fileToUpload[i].name == files.item(index).name) {
            this.jsonApiService.message('上传文件重复!')
            isSame = true
            break
          }
        }
        if (!isSame) {
          this.fileToUpload.push(files.item(index))
        }
      }
    }
  }
  // excel导入submit
  submitData(val) {
    if (this.fileToUpload.length == 0) {
      this.jsonApiService.message('请上传文件')
      return false
    }
    for (let i = 0; i < this.fileToUpload.length; i++) {
      let postParam = new FormData()
      postParam.append('file', this.fileToUpload[i])
      this.jsonApiService.form_http('bom/v3/uploadBomExcel.html', postParam).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (i === this.fileToUpload.length - 1) {
          val.hide()
          this.fileToUpload = []
          this.load()
        }
      })
    }
  }

  //别称 excel 导入
  aliasFileUp: string = ''
  aliasFileToUpload: any = [] //上传文件回调参数
  // 清空input type=file输入框的值
  clearAliasFile() {
    this.aliasFileUp = ''
  }
  //excel文件上传change回调
  aliasFileChange(files) {
    // 这一层循环判断格式是否正确
    for (let index = 0; index < files.length; index++) {
      const isEXCEL =
        files.item(index).type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ||
        files.item(index).type === 'application/vnd.ms-excel'
      if (!isEXCEL) {
        this.jsonApiService.message('上传文件只能是 excel格式!')
        return
      } else {
        let isSame = false
        for (let i = 0; i < this.aliasFileToUpload.length; i++) {
          if (this.aliasFileToUpload[i].name == files.item(index).name) {
            this.jsonApiService.message('上传文件重复!')
            isSame = true
            break
          }
        }
        if (!isSame) {
          this.aliasFileToUpload.push(files.item(index))
        }
      }
    }
  }
  // excel导入submit
  submitAliasData(val) {
    if (this.aliasFileToUpload.length == 0) {
      this.jsonApiService.message('请上传文件')
      return false
    }
    for (let i = 0; i < this.aliasFileToUpload.length; i++) {
      let postParam = new FormData()
      postParam.append('file', this.aliasFileToUpload[i])
      this.jsonApiService.form_http('bom/v3/uploadBomAliasExcel.html', postParam).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (i === this.aliasFileToUpload.length - 1) {
          val.hide()
          this.aliasFileToUpload = []
          this.load()
        }
      })
    }
  }

  //查看详情
  detail_domCode(row, id, modal) {
    modal.show()
    this.activeSelected = id
    this.BomCodeInfo = row
  }
  // 删除dom编码
  del_domCode(row) {
    this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除').then(() => {
      let reqdata = 'id=' + row.id
      this.jsonApiService.http('bom/v3/deleteBomV3ById.html', reqdata).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          this.load()
        }
      })
    })
  }
  // 分页回调
  paginate(e) {
    this.activeSelected = ''
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }
  // 刷新表
  refresh() {
    this.load()
  }

  closeDetailModal(modal) {
    modal.hide()
  }
}
